package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.LendList;
import com.haojiu.pojo.TypeInfo;
import com.haojiu.pojo.User;
import com.haojiu.service.TypeInfoService;
import com.haojiu.service.UserService;
import com.haojiu.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/27 15:57
 * create in RuGao JiangSu
 */
@Controller
public class TypeInfoController {
    @Autowired
    @Qualifier("TypeInfoServiceImpl")
    private TypeInfoService typeInfoService;

    @RequestMapping("/typeIndex")
    public ModelAndView queryAllType(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> list = typeInfoService.getAllTypeInfo(page, size);
        PageInfo<TypeInfo> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        HttpSession session = request.getSession();
        String userType = (String) session.getAttribute("userType");
        if(userType.equals("manager")){
            mv.setViewName("type/allTypeInfo");
        }else {
            mv.setViewName("type/allTypeInfo1");
        }
        return mv;
    }
    @RequestMapping("/login/delTypeInfo/{id}")
    public String deleteTypeInfo(@PathVariable("id") int id) {
        typeInfoService.delTypeInfo(id);
        return "redirect:/typeIndex";
    }

    @RequestMapping("/login/toUpdateTypeInfo")
    public String toUpdateTypeInfo(Model model, int id) {
        TypeInfo typeInfo = typeInfoService.getTypeInfo2(id);
        System.out.println(typeInfo);
        model.addAttribute("typeInfo", typeInfo);
        return "type/updateTypeInfo";
    }

    @RequestMapping("/login/updateTypeInfo")
    public String updateTypeInfo(Model model, TypeInfo typeInfo) {
        System.out.println(typeInfo);

        typeInfoService.updateTypeInfo(typeInfo);
        TypeInfo typeInfo1 = typeInfoService.getTypeInfo2(typeInfo.getId());
        System.out.println(typeInfo1);
        model.addAttribute("typeInfo1", typeInfo1);
        return "redirect:/typeIndex";
    }

    @RequestMapping("/login/toAddTypeInfo")
    public String toAddUser() {
        return "type/addTypeInfo";
    }

    @RequestMapping("/login/addTypeInfo")
    public String addTypeInfo(TypeInfo typeInfo) {
        System.out.println(typeInfo);
        typeInfoService.addTypeInfo(typeInfo);
        return "redirect:/typeIndex";
    }

    @RequestMapping("/login/searchTypeInfo")
    public String getUser(@RequestParam("keyTypeName") String typeName, Model model) {
        System.out.println(typeName);
        List<TypeInfo> typeInfo = typeInfoService.getTypeInfo(typeName);
        if (typeInfo != null) {
            model.addAttribute("one", typeInfo);
            return "type/oneTypeInfo";
        } else {
            return "redirect:/typeIndex";
        }
    }
    @RequestMapping("/login/exportTypeInfo")
    public void export(HttpServletRequest request, HttpServletResponse response){
        List<TypeInfo> typeInfoList = typeInfoService.getAllTypeInfo();
        ExportExcel<TypeInfo> ee= new ExportExcel<TypeInfo>();
        String[] headers = {"id","类型","介绍"};
        String fileName = "阅读之家图书类型excel表";
        ee.exportExcel(headers,typeInfoList,fileName,response);
    }
}
